#-*- coding: utf-8 -*-
'''
Created on Aug 27, 2012

@author: bbg
'''

from sqlalchemy import Integer, Column, String, MetaData, Table, DateTime
from sqlalchemy.orm import mapper
    
class OrderDishes(object):

    _Mapper_ = None
    _company_id_ = -1
    _shop_id_ = -1    
    def __init__(self):
        self.order_id = ''
        self.inner_rank_id = 0
        self.dish_id = 0
        self.dish_count = 0
        self.dish_price = 0
        self.dish_unit = ''
        self.unit_inner_rank_id = 0
        self.is_free = 0
        self.is_cook = 0
        self.order_mode = 0
        self.order_man = ''
        self.operator = ''
        self.discount_rate = 0
        self.is_service_charge = 0
        self.cook_content = ''
        self.cook_extra_charge = 0
        self.original_price = 0
        self.order_time = ''
        self.printer_name = ''
        self.stat_class = ''
        self.dish_name = ''
        self.create_time = ''
        self.modify_time = ''
        
    @staticmethod
    def doMapping(company_id, shop_id, year):
        '''
        按company_id分库，shop_id对应公司分店，每次生成对象前都要执行一次，以便map到正确的数据库上去
        '''
        if OrderDishes._company_id_ == company_id and OrderDishes._shop_id_ == shop_id:
            return
                
        if OrderDishes._Mapper_ is not None:
            OrderDishes._Mapper_.dispose()
            
        metaData = MetaData()
        tableName = 'order_dishes_%s' % year
        orderDishesTable = Table(tableName, metaData,
                           Column('order_id', String, primary_key = True),
                           Column('inner_rank_id', Integer, primary_key = True),
                           Column('dish_id', Integer),
                           Column('dish_count', Integer),
                           Column('dish_price', Integer),
                           Column('dish_unit', String),
                           Column('unit_inner_rank_id', Integer),
                           Column('is_free', Integer),
                           Column('is_cook', Integer),
                           Column('order_mode', Integer),
                           Column('order_man', String),
                           Column('operator', String),
                           Column('discount_rate', Integer),
                           Column('is_service_charge', Integer),
                           Column('cook_content', String),
                           Column('cook_extra_charge', Integer),
                           Column('original_price', Integer),
                           Column('order_time', DateTime),
                           Column('printer_name', String),
                           Column('stat_class', String),
                           Column('dish_name', String),
                           Column('create_time', String),
                           Column('modify_time', String),                           
                           schema = "shop_order_%s_%s" % (company_id, shop_id))
        
        OrderDishes._Mapper_ = mapper(OrderDishes, orderDishesTable)
    